package com.amigo.online.provider.merchant.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.param.result.merchant.MerchantCacheInfo;
import com.amigo.online.provider.merchant.common.ConstantValue;
import com.amigo.online.provider.merchant.common.MerchantRedisKey;
import com.amigo.online.provider.merchant.common.RwRedisService;
import com.amigo.online.provider.merchant.exception.MerchantException;
import com.amigo.online.provider.merchant.mapper.GoodsMapper;
import com.amigo.online.provider.merchant.mapper.MerchantManagerMapper;
import com.amigo.online.provider.merchant.mapper.MerchantMapper;
import com.amigo.online.provider.merchant.service.CommonCheckService;


@Service
public class CommonCheckServiceImpl implements CommonCheckService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    private RwRedisService redisService;

    @Autowired
    MerchantMapper merchantMapper;

    @Autowired
    MerchantManagerMapper merchantManagerMapper;

    @SuppressWarnings("unchecked")
    public void checkUserIsManager(long userId, long merchantId) {
        if (!redisService.sisMember(MerchantRedisKey.MERCHANT_MANAGER_SET + merchantId, userId)) {
            //List<Long> managerIds = merchantManagerMapper.listMerchantManager(merchantId);
            Long managerUserId = merchantManagerMapper.checkUserIfManager(merchantId, userId);
            if (managerUserId != null) {
                redisService.opsForSet().add(MerchantRedisKey.MERCHANT_MANAGER_SET + merchantId, userId);
            } else {
                managerUserId = merchantMapper.checkUserIfOwner(merchantId, userId);
                if (managerUserId == null) {
                    throw new MerchantException(StatusCode.MERCHANT_NOT_MANAGER);
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    public void checkGoodsIsMerchantGoods(long merchantId, long goodsId) {
        if (!redisService.sisMember(MerchantRedisKey.MERCHANT_GOODS_SET + merchantId, goodsId)) {
            //Long managerUserId = merchantManagerMapper.checkUserIfManager(merchantId, userId);
            Long merchantGoodsId = goodsMapper.checkGoodsIsMerchantGoods(merchantId, goodsId);
            if (merchantGoodsId != null) {
                redisService.opsForSet().add(MerchantRedisKey.MERCHANT_GOODS_SET + merchantId, goodsId);
            } else {
                throw new MerchantException(StatusCode.GOODS_NOT_BELONG_THIS_MERCHANT);
            }
        }
    }

    public void checkMerchantIfExist(Long merchantId) {
        MerchantCacheInfo merchantCacheInfo = (MerchantCacheInfo) redisService.get(MerchantRedisKey.MERCHANT_CACHE_INFO + merchantId);
        if (merchantCacheInfo == null) {
            merchantCacheInfo = merchantMapper.findMerchantCacheInfoById(merchantId);
            if (merchantCacheInfo == null) {
                throw new MerchantException(StatusCode.MERCHANT_NOT_EXIST);
            } else {
                if (merchantCacheInfo.getStatus() != ConstantValue.MERCHANT_APPROVALED_SUCCESS) {
                    throw new MerchantException(StatusCode.MERCHANT_EXCEPTION);
                }
                redisService.set(MerchantRedisKey.MERCHANT_CACHE_INFO + merchantId, merchantCacheInfo);
            }
        }
    }

}
